﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SaleLibrary.Sale;
using NHibernate;
using System.Data;
using BrokerBase.Lib.Tool;

namespace SaleLibrary.Sale
{

    public enum VisitTypeEnum{Connected=1,DownLoadData=2,DownLoadScore=3};

    public class VisitRecord
    {

        public static void Record(string name, VisitTypeEnum visitType, DateTime visitTime, string ipAddress,ISession sess)
        {
            IDbCommand cmd = null;
            ITransaction tx = null;

            try
            {

                cmd = sess.Connection.CreateCommand();

                tx = sess.BeginTransaction();

                sess.Transaction.Enlist(cmd);

                cmd.CommandText = "insert into sale_visitRecord (clientid,visittype,visittime,ipaddress) select c.Id,@VisitType,@VisitTime,@IpAddress  from sale_client as c where c.Name=@UserName";

                IDbDataParameter p1 = cmd.CreateParameter();
                p1.ParameterName = "@VisitType";
                p1.DbType = DbType.Int32;
                p1.Value = visitType;
                cmd.Parameters.Add(p1);

                IDbDataParameter p2 = cmd.CreateParameter();
                p2.ParameterName = "@VisitTime";
                p2.DbType = DbType.DateTime;
                p2.Value = visitTime;
                cmd.Parameters.Add(p2);

                IDbDataParameter p3 = cmd.CreateParameter();
                p3.ParameterName = "@IpAddress";
                p3.DbType = DbType.String;
                p3.Value = ipAddress;
                cmd.Parameters.Add(p3);

                IDbDataParameter p4 = cmd.CreateParameter();
                p4.ParameterName = "@UserName";
                p4.DbType = DbType.String;
                p4.Value = name;
                cmd.Parameters.Add(p4);

                cmd.ExecuteNonQuery();

                tx.Commit();
            }
            catch
            {
               //不做处理
            }
        }

        public virtual int Id { get; set; }

        public virtual Client Client { get; set; }

        public virtual DateTime VisitTime { get; set; }

        public virtual VisitTypeEnum VisitType { get; set; }

        public virtual string IpAddress { get; set; }
    }
}
